using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace hemsidan.change
{
	/// <summary>
	/// Summary description for ManageLinks.
	/// </summary>
	public partial class ManageLinks : Bases.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
				LoadData();
		}

		private void LoadData()
		{
			DataTable links = Entities.Links.GetAll();
			DataTable Categories = Entities.Links.GetCategories();

			dgLinks.DataSource = links;
			dgLinks.DataBind();

			ddlCategory.DataSource = Categories;
			ddlCategory.DataBind();
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Requi	red method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgLinks.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgLinks_EditCommand);
			this.dgLinks.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgLinks_DeleteCommand);

		}
		#endregion

		protected void btnSave_Click(object sender, System.EventArgs e)
		{
			string Category = txtNewCategory.Text;
			if (Category.Trim() == string.Empty)
				Category = ddlCategory.SelectedValue;
			if (txtLinkId.Text == "0")
			{
				Entities.Links.CreateLink(txtName.Text, txtDescription.Text, txtAddress.Text, Category);
			}
			else
			{
				Entities.Links.ChangeLink(int.Parse(txtLinkId.Text), txtName.Text, txtDescription.Text, txtAddress.Text, Category);
			}

			ClearEditPart();
		
			LoadData();
		}

		private void dgLinks_DeleteCommand(object source, DataGridCommandEventArgs e)
		{
			int LinkId = Convert.ToInt32(dgLinks.DataKeys[e.Item.ItemIndex]);
			Entities.Links.DeleteLink(LinkId);
			LoadData();
		}

		private void dgLinks_EditCommand(object source, DataGridCommandEventArgs e)
		{
			int LinkId = Convert.ToInt32(dgLinks.DataKeys[e.Item.ItemIndex]);
			txtLinkId.Text = LinkId.ToString();
			txtName.Text = e.Item.Cells[0].Text;
			txtDescription.Text = e.Item.Cells[1].Text;
			txtAddress.Text = e.Item.Cells[2].Text;
			ListItem l = ddlCategory.Items.FindByValue(e.Item.Cells[3].Text);
			ddlCategory.SelectedItem.Selected = false;
			if (l != null)
				l.Selected = true;
		}

		protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			ClearEditPart();
		}

		private void ClearEditPart()
		{
			txtLinkId.Text = "0";
			txtName.Text = "";
			txtDescription.Text = "";
			txtAddress.Text = "";
			txtNewCategory.Text = "";
		}
	}
}
